﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WITD
{
    /// <summary>
    /// Interaction logic for Opaque.xaml
    /// </summary>
    public partial class Opaque : Window
    {
        Window OwningWindow;
        bool IsStarting = true;
        bool IsOwnerSet = false;

        /// <summary>
        /// Constructor: Creates an instance of the Opaque Window class.
        /// <para></para><para></para>
        /// Shows and hides it once with zero size to ensure it can be immediately assigned to a Window's Owner property.
        /// <para></para>
        /// Sets it's dimensions equal to the parent Window's.
        /// </summary>
        /// <param name="parent"></param>
        public Opaque(Window parent)
        {
            InitializeComponent();
            OwningWindow = parent;
            //do this so other windows can assign this to their Owner property
            Width = 0;
            Height = 0;
            Show();
            Hide();

            this.Left = OwningWindow.Left;
            this.Top = OwningWindow.Top;
            this.Height = OwningWindow.Height;
            this.Width = OwningWindow.Width;

            IsStarting = false;
        }

        /// <summary>
        /// Occurs when the visibility property of this instance changes, indicating it is being shown or being hidden. When it is being 
        /// made visible for the first time, it's Owner property is set to the parent because it is necessarily true that the parent Window is being 
        /// shown.
        /// <para></para><para></para>
        /// It is the responsibility of the calling Window to disable itself when showing this Window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (IsOwnerSet) { return; }
            if (this.Visibility == System.Windows.Visibility.Visible && !IsStarting)
            {
                IsOwnerSet = true;
                Owner = OwningWindow;
            }
                
        }

    }
}
